/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import utf8 from './utf8'
let count=1000;
function encode_test(){
    let d;
    let str='\xED\xB0\x80\xF0\x9D\x8C\x86\xED\xB0\x80\xED\xB0\x80\xED\xB0\x80\xED\xA0\x80\xF0\x9D\x8C\x86\xED\xA0\x80\xED\xA0\x80\xED\xA0\x80\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81\uD800\uDC01\xA9';
    let start=Date.now();
    for(let i=0;i<count;i++){
        d=utf8.encode(str);
    }
    let end=Date.now();
    print(`utf8_encode: ${end-start} ms`);
    // print(d);//Ã­Â°ÂÃ°ÂÂÃ­Â°ÂÃ­Â°ÂÃ­Â°ÂÃ­Â ÂÃ°ÂÂÃ­Â ÂÃ­Â ÂÃ­Â ÂÃ¤Â½Â Ã¥Â¥Â½Ã¯Â¼ÂÃ¤Â¸ÂÃ§ÂÂÃ¯Â¼ÂðÂ©
}

function decode_test(){
    let d;
    let str='\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81\xC2\xA9\xF0\x90\x80\x81'
    let start=Date.now();
    for(let i=0;i<count;i++){
        d=utf8.decode(str);
    };
    let end=Date.now();
    if(d!='你好，世界！©𐀁'){
        throw new Error('utf8_decode err')
    }
    print(`utf8_decode: ${end-start} ms`);
    // print(d);
}

let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    encode_test()
    decode_test()
}

ArkTools.waitAllJitCompileFinish();

encode_test()
decode_test()

